﻿using Abp.Application.Editions;
using Abp.Application.Features;
using Abp.Auditing;
using Abp.Authorization;
using Abp.Authorization.Users;
using Abp.EntityHistory;
using Abp.Localization;
using Abp.Notifications;
using Abp.Organizations;
using Abp.UI.Inputs;
using ARchGL.Platform.Auditing.Dto;
using ARchGL.Platform.Authorization.Accounts.Dto;
using ARchGL.Platform.Authorization.Permissions.Dto;
using ARchGL.Platform.Authorization.Roles;
using ARchGL.Platform.Authorization.Roles.Dto;
using ARchGL.Platform.Authorization.Users;
using ARchGL.Platform.Authorization.Users.Dto;
using ARchGL.Platform.Authorization.Users.Profile.Dto;
using ARchGL.Platform.Chat;
using ARchGL.Platform.Chat.Dto;
using ARchGL.Platform.Core;
using ARchGL.Platform.Dtos;
using ARchGL.Platform.Editions;
using ARchGL.Platform.Editions.Dto;
using ARchGL.Platform.Friendships;
using ARchGL.Platform.Friendships.Cache;
using ARchGL.Platform.Friendships.Dto;
using ARchGL.Platform.Juneng.ViewDtos;
using ARchGL.Platform.Localization.Dto;
using ARchGL.Platform.MultiTenancy;
using ARchGL.Platform.MultiTenancy.Dto;
using ARchGL.Platform.MultiTenancy.HostDashboard.Dto;
using ARchGL.Platform.MultiTenancy.Payments;
using ARchGL.Platform.MultiTenancy.Payments.Dto;
using ARchGL.Platform.Notifications.Dto;
using ARchGL.Platform.Organizations.Dto;
using ARchGL.Platform.Qianbian;
using ARchGL.Platform.Sessions.Dto;
using ARchGL.Platform.ThirdParty.Device.InputModels;
using ARchGL.Platform.ThirdParty.Device.ViewDtos;
using AutoMapper;
using Newtonsoft.Json;

namespace ARchGL.Platform
{
    internal static class CustomDtoMapper
    {
        public static void CreateMappings(IMapperConfigurationExpression configuration)
        {
            #region 系统原有映射

            //Inputs
            configuration.CreateMap<CheckboxInputType, FeatureInputTypeDto>();
            configuration.CreateMap<SingleLineStringInputType, FeatureInputTypeDto>();
            configuration.CreateMap<ComboboxInputType, FeatureInputTypeDto>();
            configuration.CreateMap<IInputType, FeatureInputTypeDto>()
                .Include<CheckboxInputType, FeatureInputTypeDto>()
                .Include<SingleLineStringInputType, FeatureInputTypeDto>()
                .Include<ComboboxInputType, FeatureInputTypeDto>();
            configuration.CreateMap<StaticLocalizableComboboxItemSource, LocalizableComboboxItemSourceDto>();
            configuration.CreateMap<ILocalizableComboboxItemSource, LocalizableComboboxItemSourceDto>()
                .Include<StaticLocalizableComboboxItemSource, LocalizableComboboxItemSourceDto>();
            configuration.CreateMap<LocalizableComboboxItem, LocalizableComboboxItemDto>();
            configuration.CreateMap<ILocalizableComboboxItem, LocalizableComboboxItemDto>()
                .Include<LocalizableComboboxItem, LocalizableComboboxItemDto>();

            //Chat
            configuration.CreateMap<ChatMessage, ChatMessageDto>();
            configuration.CreateMap<ChatMessage, ChatMessageExportDto>();

            //Feature
            configuration.CreateMap<FlatFeatureSelectDto, Feature>().ReverseMap();
            configuration.CreateMap<Feature, FlatFeatureDto>();

            //Role
            configuration.CreateMap<RoleEditDto, Role>().ReverseMap();
            configuration.CreateMap<Role, RoleListDto>();
            configuration.CreateMap<UserRole, UserRoleExtend>();
            configuration.CreateMap<UserRoleExtend, UserRole>();
            configuration.CreateMap<UserRole, UserListRoleDto>().ReverseMap();
            configuration.CreateMap<UserRoleExtend, UserListRoleDto>();

            //Edition
            configuration.CreateMap<EditionEditDto, SubscribableEdition>().ReverseMap();
            configuration.CreateMap<EditionSelectDto, SubscribableEdition>().ReverseMap();
            configuration.CreateMap<SubscribableEdition, EditionInfoDto>();

            configuration.CreateMap<Edition, EditionInfoDto>().Include<SubscribableEdition, EditionInfoDto>();

            configuration.CreateMap<Edition, EditionListDto>();
            configuration.CreateMap<Edition, EditionEditDto>();
            configuration.CreateMap<Edition, SubscribableEdition>();
            configuration.CreateMap<Edition, EditionSelectDto>();

            //Payment
            configuration.CreateMap<SubscriptionPaymentDto, SubscriptionPayment>().ReverseMap();
            configuration.CreateMap<SubscriptionPaymentListDto, SubscriptionPayment>().ReverseMap();
            configuration.CreateMap<SubscriptionPayment, SubscriptionPaymentInfoDto>();

            //Permission
            configuration.CreateMap<Permission, FlatPermissionDto>();
            configuration.CreateMap<Permission, FlatPermissionWithLevelDto>();

            //Language
            configuration.CreateMap<ApplicationLanguage, ApplicationLanguageEditDto>();
            configuration.CreateMap<ApplicationLanguage, ApplicationLanguageListDto>();
            configuration.CreateMap<NotificationDefinition, NotificationSubscriptionWithDisplayNameDto>();
            configuration.CreateMap<ApplicationLanguage, ApplicationLanguageEditDto>()
                .ForMember(ldto => ldto.IsEnabled, options => options.MapFrom(l => !l.IsDisabled));

            //Tenant
            configuration.CreateMap<Tenant, RecentTenant>();
            configuration.CreateMap<Tenant, TenantLoginInfoDto>();
            configuration.CreateMap<Tenant, TenantListDto>();
            configuration.CreateMap<TenantEditDto, Tenant>().ReverseMap();
            configuration.CreateMap<CurrentTenantInfoDto, Tenant>().ReverseMap();

            //User
            configuration.CreateMap<User, UserEditDto>()
                .ForMember(dto => dto.Password, options => options.Ignore())
                .ReverseMap()
                .ForMember(user => user.Password, options => options.Ignore());
            configuration.CreateMap<User, UserLoginInfoDto>();
            configuration.CreateMap<User, UserListDto>();
            configuration.CreateMap<User, ChatUserDto>();
            configuration.CreateMap<User, OrganizationUnitUserListDto>();
            configuration.CreateMap<CurrentUserProfileEditDto, User>().ReverseMap();
            configuration.CreateMap<UserLoginAttemptDto, UserLoginAttempt>().ReverseMap();

            //AuditLog
            configuration.CreateMap<AuditLog, AuditLogListDto>();
            configuration.CreateMap<EntityChange, EntityChangeListDto>();

            //Friendship
            configuration.CreateMap<Friendship, FriendDto>();
            configuration.CreateMap<FriendCacheItem, FriendDto>();

            //OrganizationUnit
            configuration.CreateMap<OrganizationUnit, OrganizationUnitDto>().ReverseMap();
            configuration.CreateMap<OrganizationUnitExtend, OrganizationUnitDto>().ReverseMap();
            configuration.CreateMap<OrganizationUnit, OrganizationUnitExtend>().ReverseMap();
            configuration.CreateMap<OrganizationUnitExtend, CreateOrganizationUnitInput>().ReverseMap();

            #endregion

            /* ADD YOUR OWN CUSTOM AUTOMAPPER MAPPINGS HERE */

            #region 自定义映射

            configuration.CreateMap<AppVersions, AppVersionDto>().ReverseMap();

            configuration.CreateMap<SecurityChecks, SelfChecksInput>().ReverseMap();
            configuration.CreateMap<SecurityChecks, SecurityCheckDto>()
                .ForMember(dto => dto.DisplayNumber, opt => opt.MapFrom(entity => entity.Number.ToString().PadLeft(8, '0')))
                .ReverseMap();
            configuration.CreateMap<SecurityChecks, SecurityChecksInput>().ReverseMap();
            configuration.CreateMap<SecurityChecks, SecurityCheckDetailDto>()
                .ForMember(dto => dto.DisplayNumber, opt => opt.MapFrom(entity => entity.Number.ToString().PadLeft(8, '0')))
                .ReverseMap();
            configuration.CreateMap<SecurityCheckItems, SecurityCheckItemsDto>().ReverseMap();

            configuration.CreateMap<SecurityCheckItems, SecurityCheckItemsInput>().ReverseMap();

            configuration.CreateMap<SecurityCheckAndFeedbacks, SecurityCheckAndFeedbacksInput>().ReverseMap();
            configuration.CreateMap<SecurityCheckAndFeedbacks, SecurityCheckAndFeedbacksDto>().ReverseMap();

            configuration.CreateMap<SelfChecks, SelfChecksInput>().ReverseMap();
            configuration.CreateMap<SelfChecks, SelfChecksDto>()
                .ForMember(dto => dto.DisplayNumber, opt => opt.MapFrom(entity => entity.Number.ToString().PadLeft(8, '0')))
                .ReverseMap();

            //configuration.CreateMap<ProjectDrawings, DrawingsDto>().ReverseMap();

            configuration.CreateMap<Notices, CreateNoticesInput>().ReverseMap();
            configuration.CreateMap<Notices, NoticeDetailDto>().ReverseMap();
            configuration.CreateMap<Notices, NoticesDto>().ReverseMap();

            configuration.CreateMap<Projects, ProjectsDto>().ReverseMap();
            configuration.CreateMap<Projects, ProjectDetailsDto>().ReverseMap();
            configuration.CreateMap<ProjectUsersDto, User>().ReverseMap();
            configuration.CreateMap<ProjectUsers, AddOrModifyUserToProjectInput>().ReverseMap();
            configuration.CreateMap<ProjectModels, ProjectModelsDto>().ReverseMap();
            configuration.CreateMap<ProjectDrawings, ProjectModelsDto>().ReverseMap();
            configuration.CreateMap<ProjectModelGroups, ProjectModelGroupsDto>().ReverseMap();
            configuration.CreateMap<ProjectViewPorts, ProjectViewPortsInput>().ReverseMap();
            configuration.CreateMap<ProjectViewPorts, ProjectViewPortsDto>().ReverseMap();

            configuration.CreateMap<DocumentLibrarys, CreateFileInput>().ReverseMap();
            configuration.CreateMap<DocumentLibrarys, CreateFolderInput>().ReverseMap();
            configuration.CreateMap<DocumentLibrarys, DocumentLibrarysDto>().ReverseMap();
            configuration.CreateMap<DocumentLibrarys, FolderDto>().ReverseMap();
            configuration.CreateMap<DocumentLibrarysDto, FolderDto>().ReverseMap();
            configuration.CreateMap<DocumentLibrarysDtoForCheckP, FolderDtoForCheckP>().ReverseMap();
            configuration.CreateMap<DocumentLibrarys, DocumentLibrarysDtoForCheckP>().ReverseMap();
            configuration.CreateMap<DocumentLibrarys, FolderDtoForCheckP>().ReverseMap();
            configuration.CreateMap<DocumentShares, FolderDto>().ReverseMap();
            configuration.CreateMap<ProjectCategoryTemplate, ProjectCategoryTemplateDto>().ReverseMap();
            configuration.CreateMap<CreateProjectPropertiesInput, ProjectProperties>().ReverseMap();
            configuration.CreateMap<ProjectProperties, ProjectPropertiesDto>().ReverseMap();

            //千变接口数据映射
            configuration.CreateMap<QB_Projects, ProjectsApiDto>().ReverseMap();
            configuration.CreateMap<QB_ProjectUsers, ProjectUsersApiDto>().ReverseMap();
            configuration.CreateMap<QB_ProjectVideos, ProjectVideosApiDto>().ReverseMap();
            configuration.CreateMap<QB_ProjectLifts, ProjectLiftsApiDto>()
                .ReverseMap()
               .ForMember(entity => entity.LoadTag, opt => opt.MapFrom(dto => dto.LoadTag != null ? JsonConvert.SerializeObject(dto.LoadTag) : ""))
                .ForMember(entity => entity.StartHeightTag, opt => opt.MapFrom(dto => dto.StartHeightTag != null ? JsonConvert.SerializeObject(dto.StartHeightTag) : ""))
                .ForMember(entity => entity.StopHeightTag, opt => opt.MapFrom(dto => dto.StopHeightTag != null ? JsonConvert.SerializeObject(dto.StopHeightTag) : ""))
                .ForMember(entity => entity.SpeedTag, opt => opt.MapFrom(dto => dto.SpeedTag != null ? JsonConvert.SerializeObject(dto.SpeedTag) : ""));
            configuration.CreateMap<QB_ProjectCranes, ProjectCranesApiDto>()
                .ReverseMap()
                .ForMember(entity => entity.HeightTag, opt => opt.MapFrom(dto => dto.HeightTag != null ? JsonConvert.SerializeObject(dto.HeightTag) : ""))
                .ForMember(entity => entity.ObliquityTag, opt => opt.MapFrom(dto => dto.ObliquityTag != null ? JsonConvert.SerializeObject(dto.ObliquityTag) : ""))
                .ForMember(entity => entity.WindSpeedTag, opt => opt.MapFrom(dto => dto.WindSpeedTag != null ? JsonConvert.SerializeObject(dto.WindSpeedTag) : ""))
                .ForMember(entity => entity.RadiusTag, opt => opt.MapFrom(dto => dto.RadiusTag != null ? JsonConvert.SerializeObject(dto.RadiusTag) : ""))
                .ForMember(entity => entity.AngleTag, opt => opt.MapFrom(dto => dto.AngleTag != null ? JsonConvert.SerializeObject(dto.AngleTag) : ""))
                .ForMember(entity => entity.LoadTag, opt => opt.MapFrom(dto => dto.LoadTag != null ? JsonConvert.SerializeObject(dto.LoadTag) : ""));

            configuration.CreateMap<QB_ProjectDevices, ProjectDevicesApiDto>().ReverseMap();
            configuration.CreateMap<QB_ProjectEnvironments, ProjectEnvironmentsApiDto>()
                .ReverseMap()
                .ForMember(entity => entity.NoiseTag, opt => opt.MapFrom(dto => dto.NoiseTag != null ? JsonConvert.SerializeObject(dto.NoiseTag) : ""))
                .ForMember(entity => entity.Pm10Tag, opt => opt.MapFrom(dto => dto.Pm10Tag != null ? JsonConvert.SerializeObject(dto.Pm10Tag) : ""))
                .ForMember(entity => entity.Pm2p5Tag, opt => opt.MapFrom(dto => dto.Pm2p5Tag != null ? JsonConvert.SerializeObject(dto.Pm2p5Tag) : ""))
                .ForMember(entity => entity.WindSpeedTag, opt => opt.MapFrom(dto => dto.WindSpeedTag != null ? JsonConvert.SerializeObject(dto.WindSpeedTag) : ""))
                .ForMember(entity => entity.TemperatureTag, opt => opt.MapFrom(dto => dto.TemperatureTag != null ? JsonConvert.SerializeObject(dto.TemperatureTag) : ""))
                .ForMember(entity => entity.HumidityTag, opt => opt.MapFrom(dto => dto.HumidityTag != null ? JsonConvert.SerializeObject(dto.HumidityTag) : ""));
            configuration.CreateMap<QB_ProjectAttendances, ProjectAttendancesApiDto>().ReverseMap();
            configuration.CreateMap<QB_ProjectVideoPlays, ProjectVideoPlaysApiDto>().ReverseMap();

            //起重机
            configuration.CreateMap<Hoists, HoistInfoDto>().ReverseMap();
            configuration.CreateMap<HoistRealDatas, HoistRealDeviceDataDto>().ReverseMap();

            //升降机
            configuration.CreateMap<TowerCranes, TowerCraneInfoDto>().ReverseMap();
            configuration.CreateMap<TowerCranesRealDatas, TowerCraneRealDeviceDataDto>().ReverseMap();

            //实名制
            configuration.CreateMap<ProjectUserConvertDto, TP_ProjectUsers>().ReverseMap();            
            configuration.CreateMap<ProjectUserAttendanceConvertDto, ProjectUserAttendances>().ReverseMap();
            configuration.CreateMap<TP_ProjectUsers, TP_ProjectUsersDto>().ReverseMap();
            configuration.CreateMap<ProjectUsersRegister, ProjectUserRegisterConvertDto>().ReverseMap();

            configuration.CreateMap<ProjectUsersRegister, ProjectUserRegistInput>().ReverseMap();
            configuration.CreateMap<ProjectUserAttendances, UploadAttendanceInput>().ReverseMap();

            //专业 工种
            configuration.CreateMap<Majors, MajorsDto>().ReverseMap();
            configuration.CreateMap<MajorsCreateInput, Majors>().ReverseMap();

            //班组 团队
            configuration.CreateMap<Teams, TeamsDto>().ReverseMap();
            configuration.CreateMap<TeamsCreateInput, Teams>().ReverseMap(); 

            #endregion
        }
    }
}
